package com.chushouya.order.dao.repository;

import com.general.framework.core.lang.Integers;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.order.dao.entity.ExpressLogEntity;
import com.chushouya.order.dao.mapper.ExpressLogEntityMapper;
import com.chushouya.order.dto.admin.expresslog.ExpressLogQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Repository
public class ExpressLogRepository extends BaseRepository<ExpressLogEntityMapper, ExpressLogEntity, Long> {

    public PageResult<ExpressLogEntity> page(ExpressLogQuery query) {


        return selectPage(query, buildExample(query));
    }

    public List<ExpressLogEntity> selectList(ExpressLogQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

    private Example buildExample(ExpressLogQuery query) {
        Example example = buildExample();
        Example.Criteria c = example.createCriteria();
        if (Strings.isNotBlank(query.getExpressNo())) {
            c.andEqualTo("expressNo", query.getExpressNo());
        }
        if (Strings.isNotBlank(query.getExpressOrderNo())) {
            c.andEqualTo("expressOrderNo", query.getExpressOrderNo());
        }
        if (!Longs.isNullOrZero(query.getOrderId())) {
            c.andEqualTo("orderId", query.getOrderId());
        }
        if (!Integers.isNullOrZero(query.getBizType())) {
            c.andEqualTo("bizType", query.getBizType());
        }
        example.orderBy("acceptTime").desc();
        return example;
    }
}
